﻿using System;
using Eassets.Layered.Domain.Global;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;

namespace Eassets.Data.Global
{
    public class OrganizationLevelListData
    {
        /// <summary>
        /// Inserts organizationLevelList.
        /// </summary>
        /// <param name="organizationLevelList"></param>
        public void AddOrganizationLevelList(OrganizationLevelList organizationLevelList)
        {
            SqlCommand command = new SqlCommand("SP_AddOrganizationLevelList"); // Name of the SP
            CommonData commonData = new CommonData(); 
            commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevelList));
        }

        /// <summary>
        /// Updates organizationLevelList.
        /// </summary>
        /// <param name="organizationLevelList"></param>
        public void ModifyOrganizationLevelList(OrganizationLevelList organizationLevelList)
        {
            SqlCommand command = new SqlCommand("SP_ModifyOrganizationLevelList"); // Name of the SP
            command.Parameters.AddWithValue("@OrganizationLevelListId", organizationLevelList.OrganizationLevelListId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevelList));
        }


        /// <summary>
        /// Delete organizationLevelList.
        /// </summary>
        /// <param name="organizationLevelListId"></param>
        public void DeleteOrganizationLevelList(long organizationLevelListId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteOrganizationLevelList"); // Name of the SP
            command.Parameters.AddWithValue("@OrganizationLevelListId", organizationLevelListId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId organizationLevelList.
        /// </summary>
        /// <param name="organizationLevelListId"></param>
        /// <returns></returns>
        public OrganizationLevelList GetByIdOrganizationLevelList(long organizationLevelListId)
        {
            SqlCommand command = new SqlCommand("SP_GetOrganizationLevelListById");
            command.Parameters.AddWithValue("@OrganizationLevelListId", organizationLevelListId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            OrganizationLevelList organizationLevelList = new OrganizationLevelList();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    organizationLevelList.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelId"].ToString())))
                {
                    organizationLevelList.OrganizationLevelId = Convert.ToInt64(dataReader["OrganizationLevelId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    organizationLevelList.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    organizationLevelList.Description = Convert.ToString(dataReader["Description"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelMode"].ToString())))
                {
                    organizationLevelList.OrganizationLevelMode = Convert.ToString(dataReader["OrganizationLevelMode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyStakeholderId"].ToString())))
                {
                    organizationLevelList.CompanyStakeholderId = Convert.ToInt64(dataReader["CompanyStakeholderId"]);
                }
            }
            dataReader.Close();
            return organizationLevelList;
        }

        /// <summary>
        /// GetAll organizationLevelList.
        /// </summary>
        /// <returns></returns>
        public Collection<OrganizationLevelList> GetAllOrganizationLevelList()
        {
            Collection<OrganizationLevelList> organizationLevelLists = new Collection<OrganizationLevelList>();
            SqlCommand command = new SqlCommand("SP_GetAllOrganizationLevelLists");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            OrganizationLevelList organizationLevelList;
            while (dataReader.Read())
            {
                organizationLevelList = new OrganizationLevelList();
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    organizationLevelList.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelId"].ToString())))
                {
                    organizationLevelList.OrganizationLevelId = Convert.ToInt64(dataReader["OrganizationLevelId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    organizationLevelList.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    organizationLevelList.Description = Convert.ToString(dataReader["Description"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelMode"].ToString())))
                {
                    organizationLevelList.OrganizationLevelMode = Convert.ToString(dataReader["OrganizationLevelMode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyStakeholderId"].ToString())))
                {
                    organizationLevelList.CompanyStakeholderId = Convert.ToInt64(dataReader["CompanyStakeholderId"]);
                }

                organizationLevelLists.Add(organizationLevelList);
            }
            dataReader.Close();
            return organizationLevelLists;
        }

        public SqlCommand ConstructCommand(SqlCommand command, OrganizationLevelList organizationLevelList)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@OrganizationLevelId", organizationLevelList.OrganizationLevelId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Code", organizationLevelList.Code);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Description", organizationLevelList.Description);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@OrganizationLevelMode", organizationLevelList.OrganizationLevelMode);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@CompanyStakeholderId", organizationLevelList.CompanyStakeholderId);
            }
            catch (Exception)
            { }

            return command;
        }
    }
}

